﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.KCS
{
    public partial class SanPhamKCS_ChiTiet : Form
    {
        public static string IdMayKhoan = "0";
        public string IdSanPham = "0";

        public SanPhamKCS_ChiTiet()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
        }

        public void Loaddata()
        {
            if (IdMayKhoan != "0")
            {
                LoadGrid();
                SetPropertiesColumns();
                DataTable dt = DataAll.SelectAll("GiaCong inner join PhongMau_SanPham on GiaCong.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],GiaCong.[ID],GiaCong.[IdSanPham],GiaCong.[NgayLapLenh],GiaCong.[NguoiCapNhatLenh],GiaCong.[SoLuong],GiaCong.[NgayGuiLenh],GiaCong.[NgaySuaLenh],GiaCong.[TrangThai],GiaCong.[NgayChungTu],GiaCong.[NgayHoachToan],GiaCong.[SoChungTu],GiaCong.[GhiChu],GiaCong.[KhoDen],GiaCong.[KhoDi],GiaCong.[SXTuNgay],GiaCong.[SXDenNgay]",
                    "WHERE GiaCong.ID = " + IdMayKhoan);
                dtNgayLapLenh.Value = Convert.ToDateTime(dt.Rows[0]["NgayLapLenh"]);
                txtSoLuong.Text = dt.Rows[0]["SoLuong"].ToString();
                txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
                dtNgayChungTu.Value = Convert.ToDateTime(dt.Rows[0]["NgayChungTu"]);
                dtNgayHachToan.Value = Convert.ToDateTime(dt.Rows[0]["NgayHoachToan"]);
                txtChungtu.Text = dt.Rows[0]["SoChungTu"].ToString();
                dtSXTuNgay.Value = Convert.ToDateTime(dt.Rows[0]["SXTuNgay"]);
                dtSXDenNgay.Value = Convert.ToDateTime(dt.Rows[0]["SXDenNgay"]);

                IdSanPham = dt.Rows[0]["IdSanPham"].ToString();
                txtMaSP.Text = dt.Rows[0]["MaSanPham"].ToString();
                txtLoaiHang.Text = dt.Rows[0]["LoaiHang"].ToString();
                txtLanTai.Text = dt.Rows[0]["LanSanXuat"].ToString();
                txtSoLenh.Text = dt.Rows[0]["LenhSanXuat"].ToString();

                var data2 = DataAll.SelectAll(
                    "Cat_ChiTiet ct LEFT JOIN Kho_VatTu vt ON ct.IDVatTu = vt.IDVatTu",
                    "ct.ID,ct.[IDVatTu],vt.MaVatTu as'Mã vật tư',[S],[M],[L],[XL]," +
                    "[TongCan] as'Tổng cần', [DMTT] as'ĐMTT',[SoLuongCan] as'SL cần'," +
                    "[DauTam] as'Đầu tấm',ct.[MoTa] as'Mô tả',ct.[GhiChu] as'Ghi chú'",
                    " WHERE IDCat =" + IdMayKhoan);
                gridGiaCong.DataSource = null;
                gridGiaCong.DataSource = data2;
            }

            LoadImage();
        }

        public void LoadGrid()
        {
            var dataColumns = new DataTable();
            dataColumns.Columns.Add("Xóa");
            dataColumns.Columns.Add("Mã vật tư");
            dataColumns.Columns.Add("Mô tả");
            dataColumns.Columns.Add("S", typeof(double));
            dataColumns.Columns.Add("M", typeof(double));
            dataColumns.Columns.Add("L", typeof(double));
            dataColumns.Columns.Add("XL", typeof(double));
            dataColumns.Columns.Add("Tổng cần", typeof(double));
            dataColumns.Columns.Add("ĐMTT", typeof(double));
            dataColumns.Columns.Add("SL cần", typeof(double));
            dataColumns.Columns.Add("Đầu tấm");
            dataColumns.Columns.Add("Ghi chú");

            for (int i = 0; i < 1; i++)
            {
                dataColumns.Rows.Add("");
            }
            gridGiaCong.DataSource = null;
            gridGiaCong.DataSource = dataColumns;
        }

        public void SetPropertiesGrid()
        {
            gvGiaCong.OptionsView.ShowGroupPanel = false;
            gvGiaCong.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            gvGiaCong.OptionsView.ShowFooter = true;
            for (int i = 0; i < 8; i++)
            {
                gvGiaCong.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
            }
        }

        public void SetPropertiesColumns()
        {
            //Ma hang
            luMaVatTu.DataSource = DataAll.SelectAll("Kho_VatTu", "IDVatTu,MaVatTu,TenVatTu", "");
            luMaVatTu.Columns.Add(new LookUpColumnInfo("IDVatTu"));
            luMaVatTu.Columns.Add(new LookUpColumnInfo("MaVatTu"));
            luMaVatTu.Columns.Add(new LookUpColumnInfo("TenVatTu"));
            luMaVatTu.DisplayMember = "TenVatTu";
            luMaVatTu.ValueMember = "MaVatTu";
            luMaVatTu.NullText = string.Empty;
            luMaVatTu.Columns["IDVatTu"].Visible = false;

            //delete
            gcXoa.Width = 20;
            gcXoa.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Format columns
            gcM.DisplayFormat.FormatType = FormatType.Numeric;
            gcM.DisplayFormat.FormatString = "{0:0,0.00}";
            gcS.DisplayFormat.FormatType = FormatType.Numeric;
            gcS.DisplayFormat.FormatString = "{0:0,0.00}";
            gcL.DisplayFormat.FormatType = FormatType.Numeric;
            gcL.DisplayFormat.FormatString = "{0:0,0.00}";
            gcXL.DisplayFormat.FormatType = FormatType.Numeric;
            gcXL.DisplayFormat.FormatString = "{0:0,0.00}";
            gcDMTT.DisplayFormat.FormatType = FormatType.Numeric;
            gcDMTT.DisplayFormat.FormatString = "{0:0,0.00}";
            gcTongCan.DisplayFormat.FormatType = FormatType.Numeric;
            gcTongCan.DisplayFormat.FormatString = "{0:0,0.00}";
            gcSLCan.DisplayFormat.FormatType = FormatType.Numeric;
            gcSLCan.DisplayFormat.FormatString = "{0:0,0.00}";

            gcTongCan.OptionsColumn.AllowEdit = false;

            gcMaVatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMoTa.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcS.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcM.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcXL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTongCan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDMTT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSLCan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDauTam.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            gcS.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcM.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcXL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcTongCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcDMTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcSLCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh='" + txtMaSP.Text.Trim() + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pbAnh_Click(object sender, EventArgs e)
        {
            XemAnh.MaID = txtMaSP.Text;
            var anh = new XemAnh();
            anh.ShowDialog();
            XemAnh.MaID = "0";
        }
    }
}
